# ================================================================================================
# = Conditional acquisition of a settings property.  Returns None if the property doesn't exist. =
# ================================================================================================

def setting(setting_name,default_value=None):
    """
    Gets the setting, or None if it doesn't exist.
    """
    from django.conf import settings
    if hasattr(settings,setting_name):
        value = getattr(settings,setting_name)
        return value or default_value
    else:
        return default_value

# ================================
# = Dynamic Backends for Modules =
# ================================
def load_backend(setting_name,default_name=None):
    """
    Dynamically loads a backend module from the specified django settings
    name, or falling back to the default value if the settings is empty
    or unspecified in the settings.
    """
    backend_name = setting(setting_name,default_name)

    if backend_name:
        return __import__(backend_name,'','',[''])
    else:
        raise ImportError, 'No backend module specified for %s in Django settings, and no default available.' % setting_name